-module (login).
-compile(export_all).
-include_lib("nitrogen/include/wf.hrl").

main() -> #template { file="./site/templates/login.html" }.

%body(),showSignUp(),showLogin() generates html-code for the login page

body()->
	#panel {id=contentPlaceholder, class="contentPlaceholder", body=showLogin()}.

showSignUp() ->
	wf:wire(signupButton, userTextBox, #validate { validators=[
        #is_required { text="Username required" }
    ]}),
    wf:wire(signupButton, passTextBox, #validate { validators=[
        #is_required { text="Password required" }
    ]}),
    [
        "Enter a username and the password to sign up.",
        #p{},
        #label { text="Username" },
        #textbox { id=userTextBox, next=passTextBox },
        #p{},
        #label { text="Password" },
        #password { id=passTextBox, next=loginButton },
        #p{},
        #link { id=back, text="<- back", postback=back},
        #button { id=signupButton, text="Sign up", postback=addUser },
        #p{},
        #panel { id=mes, class="mes"}
    ].

showLogin() -> 
    wf:wire(loginButton, userTextBox, #validate { validators=[
        #is_required { text="Username required" }
    ]}),
    wf:wire(loginButton, passTextBox, #validate { validators=[
        #is_required { text="Password required" }
    ]}),
    [
        "Enter a username and the password to log in.",
        #p{},
        #label { text="Username" },
        #textbox { id=userTextBox, next=passTextBox },
        #p{},
        #label { text="Password" },
        #password { id=passTextBox, next=loginButton },
        #p{},
        #link { id=registerUser, text="Sign up", postback=register},
        #button { id=loginButton, text="Log in", postback=logIn},
        #p{},
        #panel { id=mes, class="mes"}
    ].

%% @doc Returns true if it recieves the message {ok, _}, false if it recieves {false, _} else return {error, timeOut}
%% @spec check_password() -> atom() | {error, atom()}

check_password() ->
	User = wf:q(userTextBox),
	Pass = wf:q(passTextBox),
	erlang:set_cookie(node(), '1234567890'),
	{pronection_server, pronection_server@hamberg.it.uu.se} ! {login, User, Pass, self()},
		receive
			{ok, _} ->
				true;
			{false, _} ->
				false
		after 5000 ->
			{error, timeOut}
	end.


%Following functions handles login
event(logIn) ->
    User = wf:q(userTextBox),
    case check_password() of
 		true ->
    		wf:user(User),
    		wf:redirect_from_login("/");
    	false ->
    		wf:update(mes, "Wrong username or password!");
    	{error, timeOut} ->
    		wf:update(mes, "Server timed out!")
    end;
    
event(addUser)  ->
	User = wf:q(userTextBox),
	Pass = wf:q(passTextBox),
	erlang:set_cookie(node(), '1234567890'),
	{pronection_server, pronection_server@hamberg.it.uu.se}  ! {newUser, User, Pass, self()},
		receive
			{ok, _} ->
				wf:update(mes, "Sign up complete!");
			{false, _} ->
				wf:update(mes, "Username already taken!")
		after 5000 ->
			wf:update(mes, "Server timed out!")
	end;
	
event(register) ->
	wf:update(contentPlaceholder, showSignUp());

event(back) ->
	wf:update(contentPlaceholder, showLogin()).
